﻿package render3d
{
	import flash.display.BitmapData;
	import flash.geom.ColorTransform;
	import flash.geom.Matrix3D;
	import flash.geom.Vector3D;
	import render3d.interfaces.IRender3DForObject3D;
	
	/**
	 * Universal Object3D
	 * @author Exey Panteleev
	 */
	public class CrossEngineObject3D 
	{
		protected var render:IRender3DForObject3D;
		protected var matrix:Matrix3D = new Matrix3D();
		protected var _object3D:*; // concrete Object3D of rendere Engine
		
		public function CrossEngineObject3D(render:IRender3DForObject3D, object3D:* = null):void 
		{
			this.render = render;
			_object3D = object3D
		}
		
		public function setTransform(matrix3D:Matrix3D):void 
		{
			this.matrix = matrix3D;
		}
		
		public function appendTranslation(x:Number, y:Number, z:Number):void 
		{
			matrix.appendTranslation(x, y, z);
			render.copyTransformation(matrix, _object3D)
		}		
		
		public function setPosition(x:Number, y:Number, z:Number):void 
		{
			matrix.position = new Vector3D(x, y, z);
			render.copyPosition(matrix, object3D);
		}
		
		public function setRotation(degrees:Number, axis:Vector3D):void 
		{
			matrix.prependRotation(degrees, axis);
			render.copyTransformation(matrix, _object3D)
		}
		
		public function getPosition():Vector3D 
		{
			return matrix.position
		}
		
		public function moveForward  ( distance:Number ):void 
		{ 
			translate( distance, render.FORWARD  );
			render.copyTransformation(matrix, _object3D)
		}
		
		public function moveBackward ( distance:Number ):void 
		{ 
			translate( distance, render.BACKWARD );
			render.copyTransformation(matrix, _object3D)
		}
		
		protected function translate( distance:Number, axis:Vector3D ):void
		{
			var vector:Vector3D = axis.clone();
			render.rotateAxis( this.matrix, vector );
			this.matrix.appendTranslation(distance * vector.x, distance * vector.y, distance * vector.z)
		}
		
		public function changeTexture(bitmapData:BitmapData):void 
		{
			render.changeTexture(bitmapData, this.object3D);
		}
		
		public function get object3D():* { return _object3D; }
		
	}
}